package com.merelyb.controller.account;

import com.merelyb.business.service.account.BusinessAccountInfoService;
import com.merelyb.business.service.token.BusinessTokenInfoService;
import com.merelyb.controller.Const;
import com.merelyb.data.service.service.mybatis.bean.account.AccountInfo;
import com.merelyb.data.service.service.mybatis.bean.token.TokenInfo;
import com.merelyb.util.MD5Util;
import com.merelyb.util.VerificationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by LiM on 2017/12/12.
 */
@Controller
@RequestMapping(method = RequestMethod.POST, value = "account")
public class AccountController {
    @Autowired
    BusinessAccountInfoService bussinessAccountService;
    @Autowired
    BusinessTokenInfoService businessTokenInfoService;

    /**
     * 登陆校验
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "checkLogin.do")
    public String checkAccount(HttpServletRequest request, HttpServletResponse response){
        String sPhone = request.getParameter("phone")!= null? request.getParameter("phone"):"";
        String sPwd = request.getParameter("pwd")!= null? request.getParameter("pwd"):"";
        if(sPhone.equals("")){
            request.setAttribute("message", Const.ERROR_PHONE_NULL);
            return "JSP/index";
        }
        VerificationUtils verificationUtils =new VerificationUtils();
        if(!verificationUtils.checkMobile(sPhone)){
            request.setAttribute("message", Const.ERROR_PHONE_VAILD);
            return "JSP/index";
        }
        if(sPwd.equals("")){
            request.setAttribute("message", Const.ERROR_PWD_NULL);
            return "JSP/index";
        }
        AccountInfo accountInfo = new AccountInfo();
        accountInfo.setTelNum(Long.parseLong(sPhone));
        accountInfo.setUserPwd(MD5Util.string2MD5(sPwd));
        accountInfo.setIsDelete(0);

        TokenInfo tokenInfo = bussinessAccountService.checkAccount("", "");
        if(tokenInfo == null){
            request.setAttribute("message", Const.ERROR_ACCOUNT_LOGIN);
            return "JSP/index";
        }else {
            return "JSP/mainFrame";
        }
    }

}
